package com.qf.ithub.movie_demo01.controller;

import com.qf.ithub.common.result.R;
import com.qf.ithub.common.result.ResponseEnum;
import com.qf.ithub.movie_demo01.service.impl.FileServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author : xixi
 * @Title :
 * @date :  2021-03-21 21:27
 */
@RestController
@RequestMapping("/admin")
@Api(description = "后台管理-文件接口")
@CrossOrigin("*")
@Slf4j
public class FileController {

    @Autowired
    private FileServiceImpl fileService;


    /**
     * 文件上传 只能用post
     * @param file
     */
    @ApiOperation("文件上传")
    // springmvc中 上传的文件 用这个类型MultipartFile
    // model 上传到桶中的哪个目录
    @PostMapping("/upload")
    public R upload(
            @ApiParam(value = "文件", required = true)
            @RequestParam("file") MultipartFile file,
            @ApiParam(value = "模块", required = true)
            @RequestParam("module") String module) throws IOException {

        // 获得上传文件的 InputStream
        InputStream inputStream = file.getInputStream();
        // 获得上传文件的名字
        String originalFilename = file.getOriginalFilename();
        String uploadUrl = fileService.upload(inputStream, module, originalFilename);

        //返回r对象
        return new R(ResponseEnum.SUCCESS,uploadUrl);
    }

}
